package com.example.milimili;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class FragmentBottom extends Fragment implements View.OnClickListener {
    private Button mainBtn;
    private Button myBtn;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.bottomlayout,container,false);
        mainBtn=view.findViewById(R.id.mainBtn);
        myBtn=view.findViewById(R.id.myBtn);
        mainBtn.setOnClickListener(this);
        myBtn.setOnClickListener(this);
        mainBtn.setTextColor(Color.RED);
        myBtn.setTextColor(Color.BLACK);
//        mainBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                MainActivity mainActivity=(MainActivity) getActivity();
//
//                FragmentManager fm=mainActivity.getSupportFragmentManager();
//                FragmentTransaction fragmentTransaction=fm.beginTransaction()
//            }
//        });
        return view;
    }

    @Override
    public void onClick(View view) {
        MainActivity mainActivity=(MainActivity) getActivity();
        FragmentManager fm=mainActivity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fm.beginTransaction();
        Fragment fragment=null;

        switch (view.getId())
        {
            case R.id.mainBtn:
                fragment = new MainFragment();
                setButton(0);
                break;

            case R.id.myBtn:
                fragment = new MyFragment();
                setButton(1);
                break;
        }

        // 替换操作
        fragmentTransaction.replace(R.id.frameLayout, fragment);
        // 提交
        fragmentTransaction.commit();
    }

    private void setButton(int value) {
        switch (value) {
            case 0:
                mainBtn.setTextColor(Color.RED);
                myBtn.setTextColor(Color.BLACK);
                break;
            case 1:
                myBtn.setTextColor(Color.RED);
                mainBtn.setTextColor(Color.BLACK);
                break;
        }
    }
}
